//
//  LoadingView.m
//  WeiMiLan
//
//  Created by cong on 14-8-26.
//  Copyright (c) 2014年 Mac. All rights reserved.
//

#import "LoadingView.h"
#import <QuartzCore/QuartzCore.h>

#define PudImageWidth  50
@implementation LoadingView
static LoadingView *mLoadingView = nil;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */


///初始化加载框，这个函数是表示LoadingView的大小，如果是Yes，则loadView的大小为整个窗体，在这种情况下网络请求的时候会遮盖整个窗体，用户其他操作都是无效的相当于同步，如果是No，则loadView的大小为为150*80，用户的其他操作是有效的，这种情况相下需要保证loadingView唯一；
- (id)initIsLikeSynchro:(BOOL)isLikeSynchro{
    CGRect frame = [UIApplication sharedApplication].keyWindow.rootViewController.view.frame;
    if (isLikeSynchro) {
        self = [super initWithFrame:[UIApplication sharedApplication].keyWindow.bounds];
    }else{
        //居中 锁屏位置
//        self = [super initWithFrame:CGRectMake((320-150)/2, ([UIApplication sharedApplication].keyWindow.bounds.size.height-80)/2, 150, 80)];
        
        
        //调整锁屏覆盖的范围
        self = [super initWithFrame:CGRectMake(0,44+20,frame.size.width,frame.size.height-80-20-10-10)];
    }
    
    if (self) {
        self.isLikeSynchro = isLikeSynchro;
        self.userInteractionEnabled = YES;
         UIColor *color = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.65];
        self.backgroundColor = [UIColor whiteColor];
        
        conerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 80)];
        
        [self setCenter:conerView withParentRect:self.frame];
       
        conerView.backgroundColor = [UIColor clearColor];
        [self addSubview:conerView];
        
        
        if (!olIv) {
           
//            NSString * gifPath = [[NSBundle mainBundle] pathForResource:@"mao_loading" ofType:@"gif"];
//            NSData * gifData = [NSData dataWithContentsOfFile:gifPath];
//            OLImage * gifImage = [[OLImage alloc]initWithData:gifData];
            olIv = [[OLImageView alloc]initWithFrame:CGRectMake(150/2-PudImageWidth/2., 0, PudImageWidth, (CGFloat)(ApplicationDelegate.gifImage.size.height/ApplicationDelegate.gifImage.size.width) * PudImageWidth)];
            olIv.image = ApplicationDelegate.gifImage;
        }
        
        
        
        
        [conerView addSubview:olIv];
        
//        indicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(50, 0, 50, 50)];
//        [conerView addSubview:indicatorView];
//        [indicatorView startAnimating];
        
//        UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, 150, 40)];
//        lblTitle.backgroundColor = [UIColor clearColor];
//        lblTitle.textColor = [UIColor whiteColor];
//        lblTitle.textAlignment = NSTextAlignmentCenter;
//        lblTitle.text = @"正在努力加载中...";
//        lblTitle.font = [UIFont systemFontOfSize:14];
//        [conerView addSubview:lblTitle];
        
        conerView.layer.cornerRadius = 8;
        conerView.layer.masksToBounds = YES;
        
       
        
        
    }
    return self;
}

//获得当前屏幕的Vc
-(UIViewController *)getCurrentRootViewController {
    
    
    UIViewController *result;
    
    // Try to find the root view controller programmically
    // Find the top window (that is not an alert view or other window)
    
    UIWindow *topWindow = [[UIApplication sharedApplication] keyWindow];
    if (topWindow.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
    
        for(topWindow in windows)
        {
            if (topWindow.windowLevel == UIWindowLevelNormal)
                break;
        }
    }
    UIView *rootView = [[topWindow subviews] objectAtIndex:0];
    id nextResponder = [rootView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        
        result = nextResponder;
    
    else if ([topWindow respondsToSelector:@selector(rootViewController)] && topWindow.rootViewController != nil)
        
        
        result = topWindow.rootViewController;
    
    
    else
        
        NSAssert(NO, @"ShareKit: Could not find a root view controller.  You can assign one manually by calling [[SHK currentHelper] setRootViewController:YOURROOTVIEWCONTROLLER].");
    
    return result;
}

- (void)show{
    if ([UIApplication sharedApplication].keyWindow.rootViewController.navigationController) {
        [[UIApplication sharedApplication].keyWindow.rootViewController.navigationController.view addSubview:self];
    }else{
        [[UIApplication sharedApplication].keyWindow addSubview:self];
    }
    
    //监视rootVC的变化
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(dismissWhenDisappear:) name:@"切换VC" object:nil];
}

-(void)dismissWhenDisappear:(NSNotification*)nofi{
   
    
//    if ([nofi.object isEqual:[self getCurrentRootViewController]]) {
//         [self dismiss];
//         [[NSNotificationCenter defaultCenter] removeObserver:self name:@"切换VC" object:nil];
//    }
   
}
- (void)dismiss{
    double delayInSeconds = .5;\
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);\
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [self removeFromSuperview];
    });
    
   
}

+ (LoadingView *)shareLoadingView{
    @synchronized(self){
        if (mLoadingView==nil) {
            mLoadingView = [[self alloc] initIsLikeSynchro:NO];
        }
    }
    return mLoadingView;
}

+ (id)allocWithZone:(NSZone *)zone{
    @synchronized(self){
        if (mLoadingView==nil) {
            mLoadingView = [super allocWithZone:zone];
            return mLoadingView;
        }
    }
    return  nil;
}

///设置子View在父View中居中
- (void)setCenter:(UIView *)child withParentRect:(CGRect)parentRect{
    CGRect rect = child.frame;
    rect.origin.x = (parentRect.size.width - child.frame.size.width)/2;
    rect.origin.y = (parentRect.size.height - child.frame.size.height)/2;
    child.frame = rect;
}

@end
